/**
 * Created by IntelliJ IDEA.
 * User: ReZzZ
 * Date: 07 Apr 11
 * Time: 12:09:39
 * To change this template use File | Settings | File Templates.
 */
public class MainUI {
    private ProjModel pm = new ProjModel();
    private GridView gv = new GridView();


    public void showMenu() {
        int x = 1;
        /**show the menu in the program*/
        while (x > 0) {
        System.out.print( " === MENU ===\n" +
                " 1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n" +
                " 2. Set Number of vertical lanes  (V-street) [min 1, max 4]\n" +
                " 3. Set Probability of a car entering H-street [min 0, max 1]\n" +
                " 4. Set Probability of a car entering V-street [min 0, max 1]\n" +
                " 5. Run one simulation cycle\n" +
                " 6. Set and run number of simulation cycles [min 1, max 10]\n" +
                " ");
        String menuSelection = GetUserInput.getUserChoice("Enter your choice>");
        int userChoiceInt = Integer.parseInt(menuSelection);

        switch(userChoiceInt) {
            case 1 : System.out.println("Menu 1");
                    setNumHorLanes();
                break;
            case 2 : System.out.println("Menu 2");
                    setNumVerLanes();
                break;
            case 3 : System.out.println("Menu 3");
                    setHorCProb();
                break;
            case 4 : System.out.println("Menu 4");
                    setVerCProb();
                    gv.initGrid(pm);
                break;
            case 5 : System.out.println("Menu 5");
                    runOne();
                break;
            case 6 : System.out.println("Menu 6");
                    simulationCycles() ;
                break;
            default : System.out.println("Error in entering menu choice");
                break;
        }
      }
    }
     /**to ask user the number of the horizontal lanes*/
    public void setNumHorLanes () {
        String userSelection = GetUserInput.getUserChoice("Enter number of horizontal lanes : ");
        int userSelectionInt = Integer.parseInt(userSelection);
        int lowerLimit = 1;
        int upperLimit = 3;
         /*error input checking*/
        if (userSelectionInt > upperLimit || userSelectionInt < lowerLimit) {
           System.out.println("Error in entering horizontal lanes");
           System.out.println("Please enter number above 1 and below 3. ");
           showMenu();
        } else { //if user enter the correct input
            pm.setHorLanes(userSelectionInt);
            //let user set the number
        }

    }
    /*ask user the number of vertical lanes*/
    public void setNumVerLanes () {
        String userSelection = GetUserInput.getUserChoice("Enter number of vertical lanes : ");
        int userSelectionInt = Integer.parseInt(userSelection);
        int lowerLimit = 1;
        int upperLimit = 4;
        /*error checking*/
        if (userSelectionInt > upperLimit || userSelectionInt < lowerLimit) {
           System.out.println("Error in entering vertical lanes");
           System.out.println("Please enter number above 1 and below 4. ");
           showMenu();
        } else {
            pm.setVerLanes(userSelectionInt);

        }

    }
     // set the probability the car moving in the road
    public void setHorCProb() {
        String userSelection = GetUserInput.getUserChoice("Enter probability car on Horizontal Lanes[0-1]:");
        double userSelectionDouble = Double.parseDouble(userSelection);
        // error checking
        if (userSelectionDouble > 1 || userSelectionDouble < 0) {
            System.out.println("Error! Please enter number from 0 - 1");
            showMenu();
        } else {
            pm.setHorCarProb(userSelectionDouble);
            System.out.println("Probability of horizontal car is " + pm.getHorCarProb());

        }
    }

    public void setVerCProb() {
        String userSelection = GetUserInput.getUserChoice("Enter probability car on Vertical Lanes[0-1]:");
        double userSelectionDouble = Double.parseDouble(userSelection);

        if (userSelectionDouble > 1 || userSelectionDouble < 0) {
            System.out.println("Error! Please enter number from 0 - 1");
            showMenu();
        } else {
            pm.setVerCarProb(userSelectionDouble);
            System.out.println("Probability of vertical car is " + pm.getVerCarProb());
        }
    }

    public void runOne(){
        //running the cycles once
        gv.runCars(pm, 1);
    }

    //running the menu 6 and make a custom cycles within 1 to 10 cycles
    public void simulationCycles() {

        String userSelection = GetUserInput.getUserChoice("Enter number of simulation cycle [1-10]:");
        int userSelectionInt = Integer.parseInt(userSelection);

        if (userSelectionInt <1  || userSelectionInt > 10) {
            System.out.println("Error! Insert number from 1 - 10");
            showMenu();
        } else {
             gv.runCars(pm, userSelectionInt);
        }
    }

    public int showHorLanes() {
        return pm.getHorLanes();
    }

    public int showVerLanes() {
        return pm.getVerLanes();
    }
}
